package com.ds.core.support.qiniu;

import org.apache.commons.lang3.StringUtils;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;

public class CredentialsManager {
	private static String accessKey = "z8auMe-KjObq59Z03UgqN5CeDGUCGhlihkGnNFKe";
	private static String secretKey = "xyN2I0eN4Ifj4EuafJ-19Wmkwj0JeAIsz2lW604L";
	private static String bucket = "ds-literary";
	
	public static Auth getAuth() {
		return Auth.create(accessKey, secretKey);
	}
	/**
	 * 生成上传凭证
	 */
	public static String getUploadCredential() {
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		System.out.println(upToken);
		return upToken;
	}

	/**
	 * 获取客户端覆盖上传凭证
	 * 
	 * @param fileKey 被覆盖的文件名
	 */
	public static String getOverloadCredential(String fileKey) {
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket, fileKey);
		System.out.println(upToken);
		return upToken;
	}
	
	
	/**
	 * 获取客户端上传凭证(自定义返回值)
	 * @param returnBody 自定义返回值
	 * @param expireSeconds 有效期(秒)
	 * @return
	 */
	public static String getUploadCredential(String returnBody,long expireSeconds) {
		Auth auth = Auth.create(accessKey, secretKey);
		StringMap putPolicy = new StringMap();
		putPolicy.put("returnBody",returnBody);
//		putPolicy.put("returnBody",
//				"{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
		String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
		System.out.println(upToken);
		return upToken;
	}

	/**
	 *  获取客户端上传凭证(自定义回调application/json格式)
	 * 
	 * @param callbackBody 自定义回调
	 *            application/json格式
	 * @param callbackUrl 回调地址
	 * @param expireSeconds 有效期
	 */
	public static void getUploadCredential(String callbackBody, String callbackUrl,long expireSeconds) {
		Auth auth = Auth.create(accessKey, secretKey);
		StringMap putPolicy = new StringMap();
//		putPolicy.put("callbackUrl", "http://api.example.com/qiniu/upload/callback");
//		putPolicy.put("callbackBody",
//				"{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");
		putPolicy.put("callbackUrl", callbackUrl);
		putPolicy.put("callbackBody",callbackBody);
		// putPolicy.put("callbackBody",
		// "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize),\"user\":\"$(x:user)\",\"age\",$(x:age)}");
		putPolicy.put("callbackBodyType", "application/json");
		String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
		// String upToken = auth.uploadToken(bucket, key, expireSeconds, putPolicy,
		// false);//false:允许添加额外参数
		System.out.println(upToken);
	}

	/**
	 * 获取客户端上传凭证(自定义回调application/x-www-form-urlencoded格式)
	 * 
	 * @param callbackBody 自定义回调
	 *            application/x-www-form-urlencoded格式
	 * @param callbackUrl 回调地址
	 * @param expireSeconds 有效期
	 */
	public static void getUploadCredential2(String callbackBody, String callbackUrl,long expireSeconds) {
		Auth auth = Auth.create(accessKey, secretKey);
		StringMap putPolicy = new StringMap();
//		putPolicy.put("callbackUrl", "http://api.example.com/qiniu/upload/callback");
//		putPolicy.put("callbackBody", "key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)");
		putPolicy.put("callbackUrl", callbackUrl);
		putPolicy.put("callbackBody", callbackBody);
//		long expireSeconds = 3600;
		String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
		System.out.println(upToken);
	}

	/**
	 * 获取客户端上传凭证（带数据处理的凭证）
	 */
	public static String getUploadCredentialWithDeal() {
		Auth auth = Auth.create(accessKey, secretKey);
		StringMap putPolicy = new StringMap();
		// 数据处理指令，支持多个指令
		String saveMp4Entry = String.format("%s:avthumb_test_target.mp4", bucket);
		String saveJpgEntry = String.format("%s:vframe_test_target.jpg", bucket);
		String avthumbMp4Fop = String.format("avthumb/mp4|saveas/%s", UrlSafeBase64.encodeToString(saveMp4Entry));
		String vframeJpgFop = String.format("vframe/jpg/offset/1|saveas/%s",
				UrlSafeBase64.encodeToString(saveJpgEntry));
		// 将多个数据处理指令拼接起来
		String persistentOpfs = StringUtils.join(new String[] { avthumbMp4Fop, vframeJpgFop }, ";");
		putPolicy.put("persistentOps", persistentOpfs);
		// 数据处理队列名称，必填
		putPolicy.put("persistentPipeline", "mps-pipe1");
		// 数据处理完成结果通知地址
		putPolicy.put("persistentNotifyUrl", "http://api.example.com/qiniu/pfop/notify");
		long expireSeconds = 3600;
		String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
		System.out.println(upToken);
		return upToken;
	}
	public static void main(String[] args) {
		getUploadCredential();
		getOverloadCredential("testfile");
		getUploadCredential("{\\\"key\\\":\\\"$(key)\\\",\\\"hash\\\":\\\"$(etag)\\\",\\\"bucket\\\":\\\"$(bucket)\\\",\\\"fsize\\\":$(fsize)}\"",3600);
		getUploadCredential("{\\\"key\\\":\\\"$(key)\\\",\\\"hash\\\":\\\"$(etag)\\\",\\\"bucket\\\":\\\"$(bucket)\\\",\\\"fsize\\\":$(fsize)}", "http://api.example.com/qiniu/upload/callback",3600);
		getUploadCredential2("key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)", "http://api.example.com/qiniu/upload/callback",3600);
		getUploadCredentialWithDeal();
	}
}
